/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.views;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.table.AbstractTableModel;
import pe.edu.pucp.Servicio.Servicio_Contenedor;
import pe.edu.pucp.Servicio.Servicio_ServiciosTerminal;
import pe.edu.pucp.bean.*;

/**
 *
 * @author LiziTa
 */
public class PanelContenedorBuscar extends javax.swing.JPanel {

    Servicio_Contenedor service = Servicio_Contenedor.get_instance();
    Servicio_ServiciosTerminal service2 = Servicio_ServiciosTerminal.get_instance();
    private static String idCont;
    
    public String getId(){
        return idCont;
    }
    
    private int variable;
    
    PanelContenedorBuscar.ContenedorTm miContenedorTM = new PanelContenedorBuscar.ContenedorTm();
    ServiciosTm miServicioTM= new ServiciosTm();
      
    public void actualizaTabla() {
        miContenedorTM.conts=service.queryAllContenedor();
        //miContenedorTM.conts=main.Main.service.queryAllObjectsContenedor();
        miContenedorTM.fireTableChanged(null);
    }
    
   
   
    
    class ServiciosTm extends AbstractTableModel{

        ArrayList<Servicio> servs= service2.queryAllServxCont(idCont);
        String titulos2[]={"IDSERVICIO","DESCRIPCION","PRECIO BASE","UNIDAD DE COBRO"};
        
        @Override
        public int getRowCount() {
            return servs.size();
        }

        @Override
        public int getColumnCount() {
            return 4;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            Servicio serv= servs.get(rowIndex);
                
             switch(columnIndex){
                case 0: res = "" + serv.getId();break;
                case 1:res="" + serv.getDescripcion();break;
                case 2:res=""+serv.getPreciobase();break;
                case 3:res=""+serv.getUnidadcobro();break;
             }


            return res;
        }
        
        @Override
        public String getColumnName(int col){//COLOCA LOS TITULOS DESEADOS
            return titulos2[col];
        }
       
   }
    
    class ContenedorTm extends AbstractTableModel{

        ArrayList<Contenedor> conts = service.queryAllContenedor();
        String titulos[]={"CODIGO","PESO NETO","PESO BRUTO","VOLUMEN","DESCRIPCION MERCADERIA","TARA","TIPOCONTENEDOR","MOTOR","TEMPERATURA","TIPO CONTENIDO","ESTADO"};
        
        
        @Override
        public int getRowCount() {
            return conts.size();
        }

        @Override
        public int getColumnCount() {
            return 11 ;
        }

        @Override
       public Object getValueAt(int rowIndex, int columnIndex) {            
            String res=null;
            Contenedor cont= conts.get(rowIndex);
                
             switch(columnIndex){
                 
                case 0:res = "" + cont.getId();break;
                case 1:res = "" + cont.getPeso_neto();break;
                case 2:res = "" + cont.getPeso_bruto();break;
                case 3:res="" + cont.getVolumen();break;
                case 4:res="" + cont.getDescripcionMerc();break;
                case 5:res="" + cont.getTara(); break;
                case 6:res=cont.getTipoContenedor();break;
                case 7: if (cont instanceof Refrigerado){
                    res="" + ((Refrigerado)cont).getMotor();break;
                }else {res="-"; break;}
                case 8:if (cont instanceof Refrigerado){
                    res="" + ((Refrigerado)cont).getTemperatura();break;
                }else {res="-"; break;}
                case 9:if (cont instanceof NoRefrigerado){
                    res="" + ((NoRefrigerado)cont).getTipoContenido();break;
                }else {res="-"; break;}
                case 10: if (cont instanceof NoRefrigerado){
                    res="" + ((NoRefrigerado)cont).getEstado();break;
                }else {res="-"; break;}
             }


         return res;
        }
        
        @Override
        public String getColumnName(int col){//COLOCA LOS TITULOS DESEADOS
            return titulos[col];
        }
        
    }
    
    
  public void ActualizarBusqueda(ArrayList<Contenedor> c){
        miContenedorTM.conts=c;
        miContenedorTM.fireTableChanged(null);
    }   
    
    public PanelContenedorBuscar() {
        initComponents();
        TablaContenedor.setModel(miContenedorTM);
        //TablaServiciosxContenedor.setModel(miServicioTM);
       
        actualizaTabla();
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtDescMercaderia = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaContenedor = new javax.swing.JTable();
        Buscar = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();

        jLabel1.setText("Tabla de Contenedores Tramarsa");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Contenedores"));

        jLabel6.setText("Descripción");

        txtDescMercaderia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDescMercaderiaKeyTyped(evt);
            }
        });

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/edu/pucp/views/logo.gif"))); // NOI18N

        jLabel2.setText("Código");

        txtId.setText(" ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDescMercaderia, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                    .addComponent(txtId))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel13)
                .addGap(31, 31, 31))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDescMercaderia, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(24, 24, 24))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(72, 72, 72))
        );

        TablaContenedor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablaContenedor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaContenedorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TablaContenedor);

        Buscar.setText("Buscar");
        Buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BuscarActionPerformed(evt);
            }
        });

        Cancelar.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(159, 159, 159)
                        .addComponent(Buscar)
                        .addGap(44, 44, 44)
                        .addComponent(Cancelar)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Buscar)
                    .addComponent(Cancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(111, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtDescMercaderiaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDescMercaderiaKeyTyped
    char car = evt.getKeyChar();    
if((car<'a' || car>'z') && (car<'A' || car>'Z') 
   && (car!=(char)KeyEvent.VK_SPACE))
{
   evt.consume();
   }

if(txtDescMercaderia.getText().length()>=50) evt.consume();
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDescMercaderiaKeyTyped

    private void TablaContenedorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaContenedorMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_TablaContenedorMouseClicked

    private void BuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BuscarActionPerformed
     String id=(txtId.getText()).replaceAll("^\\s+", "");
     if (txtId.getText().equals("")) id="%";
     String desc=txtDescMercaderia.getText().replaceAll("^\\s+", "");
      if (txtDescMercaderia.getText().equals("")) desc="%";
      
     ArrayList<Contenedor> result=service.queryByCampos(id,desc);
     ActualizarBusqueda(result);
        // TODO add your handling code here:
    }//GEN-LAST:event_BuscarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Buscar;
    private javax.swing.JButton Cancelar;
    private javax.swing.JTable TablaContenedor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtDescMercaderia;
    private javax.swing.JTextField txtId;
    // End of variables declaration//GEN-END:variables
}
